﻿@inherits LayoutComponentBase

@inject IJSRuntime JSRuntime
@inject MessageService _message
@inject NavigationManager navigationManager


@* <style>
    #components-layout-demo-top-side-2 .logo {
    width: 120px;
    height: 31px;
    background: rgba(255, 255, 255, 0.2);
    margin: 16px 28px 16px 0;
    float: left;
    }

    .ant-header {
    margin: 0;
    width: 100%;
    position: relative !important;
    background-image: unset !important;
    background-size: unset !important;
    height: 64px !important;
    }

    .site-layout-background {
    background: #fff;
    }
    </style> *@

<Layout style="height: 100%;">
    <Header Class="header">
        <div class="ant-header-logo" style="display: inline;">
            <img src="/logo.png" width="48" height="48" style="border-radius: 50%;" />
        </div>
        <Menu Theme="MenuTheme.Dark" Mode="MenuMode.Horizontal" style="float:right;">
            <MenuItem Key="1">
            <NavLink href="/">到前台首页</NavLink>
            </MenuItem>
            <MenuItem Key="2">
            <NavLink @onclick="Logout">退出登录</NavLink>
            </MenuItem>
        </Menu>
    </Header>
    <Layout>
        <Sider Width="200" Class="site-layout-background">
            <Menu Mode="MenuMode.Inline" DefaultSelectedKeys=@(new[] {"1"}) DefaultOpenKeys=@(new[] {"articleManage"})
                Style="height: 100%; border-right: 0;">

                <MenuItem Key="1">
                <Icon Type="desktop" Theme="outline" />
                <span>
                    <NavLink href="/admin/">控制面板</NavLink>
                </span>
                </MenuItem>

                @{
                    RenderFragment 文章管理 =
                    @<span>
                        <Icon Type="user" Theme="outline" />
                        文章管理
                    </span>
                    ;
                }
                <SubMenu Key="articleManage" Title=文章管理>
                    <MenuItem Key="2">
                    <Icon Type="table" Theme="outline" />
                    <NavLink href="/admin/articles">文章列表</NavLink>
                    </MenuItem>
                    <MenuItem Key="3">
                    <Icon Type="file-add" Theme="outline" />
                    <NavLink href="/admin/article/add">添加文章</NavLink>
                    </MenuItem>
                </SubMenu>


                @{
                    RenderFragment 分类管理 =
                    @<span>
                        <Icon Type="laptop" Theme="outline" />
                        分类管理
                    </span>
                    ;
                }
                <SubMenu Key="categoryManage" Title=分类管理>
                    <MenuItem Key="4">
                    <Icon Type="table" Theme="outline" />
                    <NavLink href="/admin/categories">分类列表</NavLink>
                    </MenuItem>
                </SubMenu>

            </Menu>
        </Sider>
        <Layout Style=" padding: 0 24px 24px;">

            @Body

        </Layout>
    </Layout>
</Layout>


@code {
    // 生命周期：参数设置后发生
    protected override async Task OnParametersSetAsync()
    {
        LocalStorageHelper localStorageHelper = new LocalStorageHelper(JSRuntime);
        string token = await localStorageHelper.GetLocalStorage("token");
        if (string.IsNullOrEmpty(token))
        {
            _message.Info("未登录，请登录后再试");
            navigationManager.NavigateTo("/admin/login", true);
        }
        await base.OnParametersSetAsync();
    }

    private async Task Logout()
    {
        LocalStorageHelper localStorageHelper = new LocalStorageHelper(JSRuntime);
        await localStorageHelper.SetLocalStorage("token", "");
        navigationManager.NavigateTo("/admin/login", true);
    }

    // 生命周期：页面渲染后发生
    //protected override async Task OnAfterRenderAsync(bool firstRender)
    //{
    // LocalStorageHelper localStorageHelper = new LocalStorageHelper(JSRuntime);
    // string token = await localStorageHelper.GetLocalStorage("token");
    // if (string.IsNullOrEmpty(token))
    // {
    // _message.Info("未登录，请登录后再试");
    // navigationManager.NavigateTo("/admin/login", true);
    // }
    // await base.OnAfterRenderAsync(firstRender);
    //}

    // 生命周期：初始化时发生
    //protected override async Task OnInitializedAsync()
    //{
    // LocalStorageHelper localStorageHelper = new LocalStorageHelper(JSRuntime);
    // string token = await localStorageHelper.GetLocalStorage("token");
    // if (string.IsNullOrEmpty(token))
    // {
    // _message.Info("未登录，请登录后再试");
    // navigationManager.NavigateTo("/admin/login", true);
    // }
    // await base.OnInitializedAsync();
    //}
}